//
//  PKVideoViewController.swift
//  GoAbroad
//
//  Created by Pink Snow on 2025/6/3.
//

import UIKit
import AVFoundation
import CoreVideo
class PKVideoViewController: UIViewController {
  

    override func viewDidLoad() {
        super.viewDidLoad()
        let composition = AVMutableComposition()
        
        let videoURL = URL(string: "")
         let asset = AVAsset(url: videoURL!)
        asset.loadTracks(withMediaType: .video) { tracks, error in
            if let error = error {
                print("加载轨道失败: \(error)")
                return
            }

            guard let videoTrack = tracks?.first else {
                print("没有找到视频轨道")
                return
            }
            let image = UIImage(named: "your_image_name")!
            let videoSize = CGSize(width: 720, height: 1280)
            let imageLayer = CALayer()
            imageLayer.contents = image.cgImage
            imageLayer.frame = CGRect(x: 0, y: videoSize.height / 2, width: videoSize.width, height: videoSize.height / 2)
            imageLayer.contentsGravity = .resizeAspectFill
            // 在这里继续你的合成逻辑
            let compositionVideoTrack = composition.addMutableTrack(withMediaType: .video,
                                                                     preferredTrackID: kCMPersistentTrackID_Invalid)
            let videoLayer = CALayer()
            videoLayer.frame = CGRect(x: 0, y: 0, width: videoSize.width, height: videoSize.height / 2)
            
            let parentLayer = CALayer()
            let overlayLayer = CALayer()

            parentLayer.frame = CGRect(x: 0, y: 0, width: videoSize.width, height: videoSize.height)
            overlayLayer.frame = parentLayer.frame

            parentLayer.addSublayer(imageLayer)
            parentLayer.addSublayer(videoLayer)

            let videoComposition = AVMutableVideoComposition()
            videoComposition.renderSize = videoSize
            videoComposition.frameDuration = CMTime(value: 1, timescale: 30)
            videoComposition.instructions = []

            let instruction = AVMutableVideoCompositionInstruction()
            instruction.timeRange = CMTimeRange(start: .zero, duration: asset.duration)

            let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: compositionVideoTrack!)
            instruction.layerInstructions = [layerInstruction]
            videoComposition.instructions = [instruction]

            videoComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer,
                                                                                 in: parentLayer)
            
            let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)!
            exportSession.outputURL = exportURL
            exportSession.outputFileType = .mp4
            exportSession.videoComposition = videoComposition

            exportSession.exportAsynchronously {
                if exportSession.status == .completed {
                    print("导出完成")
                } else {
                    print("导出失败: \(String(describing: exportSession.error))")
                }
            }
            
            
        }
         

        // Do any additional setup after loading the view.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
